package com.fmatos.amazingwallpapers.wallpaper.service;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import com.fmatos.amazingwallpapers.image.database.ImagesDataSource;
import com.fmatos.amazingwallpapers.image.database.orm.DatabaseManager;
import com.fmatos.amazingwallpapers.imagemanagement.WallpaperBitmaps;
import com.fmatos.amazingwallpapers.service.image.BitmapsWorker;
import com.fmatos.amazingwallpapers.wallpaper.domain.AmazingWallpaper;

public class AmazingWallpaperService extends WallpaperService {

	private static final String TAG = AmazingWallpaperService.class.getSimpleName();

	// TODO read from preferences
	public static final long IMAGE_UPDATE_INTERVAL = 15000; // in ms
	
	@Override
	public Engine onCreateEngine() { 
		return new AmazingWallpaperEngine();
	}

	public void onCreate() {
		Log.i(TAG,"Open database");
		DatabaseManager.acquireDatabase(this);

	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
	
		return super.onStartCommand(intent, flags, startId);
	}
	
	public void onDestroy() {
		Log.i(TAG,"Close database");
		DatabaseManager.releaseDatabase();
	}

	private class AmazingWallpaperEngine extends Engine {

		private AmazingWallpaper wallpaper;

		private boolean visible = true;

		private final Handler handler = new Handler();
		private final Runnable drawRunner = new Runnable() {
			@Override
			public void run() {
				draw();
			}

		};
		
		public AmazingWallpaperEngine() {

			SharedPreferences prefs = PreferenceManager
					.getDefaultSharedPreferences(AmazingWallpaperService.this);

			Log.i(TAG,"Creating engine");
			//			wallpaper = new WallpaperCircles(prefs);
			wallpaper = new WallpaperBitmaps(getApplicationContext());
			//			wallpaper = new WallpaperDrawing(prefs);
			handler.post(drawRunner);
		}

		@Override
		public void onVisibilityChanged(boolean visible) {
			Log.i(TAG,"visibility =" + visible);
			this.visible = visible;
			wallpaper.onVisibilityChanged(visible);
			if (visible) {
				handler.post(drawRunner);
			} else {
				handler.removeCallbacks(drawRunner);
			}
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			Log.i(TAG,"surface destroyed");
			super.onSurfaceDestroyed(holder);
			
			visible = false;
			wallpaper.onVisibilityChanged(visible);
			handler.removeCallbacks(drawRunner);
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format,
				int width, int height) {
			Log.i(TAG,"surface changed");
			wallpaper.onSurfaceChanged(holder,width,height);
			super.onSurfaceChanged(holder, format, width, height);
		}

		@Override
		public void onTouchEvent(MotionEvent event) {

			wallpaper.onTouchEvent(event,getSurfaceHolder());
			super.onTouchEvent(event);
		}

		
		

		@Override
		public void onDestroy() {
			Log.i(TAG,"engine destroyed");
			visible = false;
			wallpaper.onVisibilityChanged(visible);
			super.onDestroy();
		}

		private void draw() {
			
			wallpaper.draw(getSurfaceHolder());
			
			handler.removeCallbacks(drawRunner);
			if (visible) {
				handler.postDelayed(drawRunner, IMAGE_UPDATE_INTERVAL);
			}
		}
	} 

}
